// index.js
Page({
  data: {
    currentSceneIndex: 0,
    scenes: ['scene1.png', 'scene2.png', 'scene3.png'],
    petAttributes: {
      level: 5,
      vitality: 80,
      mood: 60
    },
    currentTodo: '',
    todos: [
      { id: 1, category: '学习', content: '学习微信小程序', time: '2023-05-20 14:00', completed: false },
      { id: 2, category: '工作', content: '完成项目报告', time: '2023-05-21 10:00', completed: true }
    ],
    categories: ['学习', '工作', '生活', '其他'],
    isTimerRunning: false,
    timerMinutes: 25,
    timerSeconds: 0
  },
  // 切换场景
  changeScene: function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      currentSceneIndex: index
    });
  },
  // 更新宠物属性
  updatePetAttributes: function() {
    // 这里应该有更新宠物属性的逻辑
  },
  // 新建待办
  inputTodo: function(e) {
    this.setData({
      currentTodo: e.detail.value
    });
  },
  // 提交待办
  submitTodo: function(e) {
    let category = e.detail.value.category;
    let content = this.data.currentTodo;
    let time = e.detail.value.time;
    let newTodo = {
      id: this.data.todos.length + 1,
      category: category,
      content: content,
      time: time,
      completed: false
    };
    this.setData({
      todos: [...this.data.todos, newTodo]
    });
    wx.showToast({
      title: '已保存',
      icon: 'success'
    });
  },
  // 切换待办状态
  toggleTodo: function(e) {
    let id = e.currentTarget.dataset.id;
    let todos = this.data.todos.map(todo => {
      if (todo.id === id) {
        todo.completed = !todo.completed;
      }
      return todo;
    });
    this.setData({ todos: todos });
  },
  // 开始/暂停番茄钟
  toggleTimer: function() {
    if (this.data.isTimerRunning) {
      clearInterval(this.timer);
    } else {
      this.startTimer();
    }
    this.setData({
      isTimerRunning: !this.data.isTimerRunning
    });
  },
  // 开始番茄钟计时
  startTimer: function() {
    this.timer = setInterval(() => {
      if (this.data.timerSeconds > 0) {
        this.setData({
          timerSeconds: this.data.timerSeconds - 1
        });
      } else if (this.data.timerMinutes > 0) {
        this.setData({
          timerMinutes: this.data.timerMinutes - 1,
          timerSeconds: 59
        });
      } else {
        clearInterval(this.timer);
        wx.showToast({
          title: '时间到！',
          icon: 'success'
        });
      }
    }, 1000);
  }
})
